#pragma once

#include <QObject>
#include <QTimer>
#include <QTime>
#include <memory>
#include "TrLoader.h"

class ContractLoader : public QObject
{
    Q_OBJECT
public:
    explicit ContractLoader(QObject *parent = nullptr);
    ~ContractLoader();

    bool init(const QString& configFile);
    void start();
    void stop();

private slots:
    void onScheduledUpdate();
    void onUpdateCompleted();
    void onUpdateError(const QString& error, int errorId = 0);
    void onLoaderConnected();
    void onLoaderDisconnected(int reason);

private:
    void scheduleNextUpdate();
    bool loadConfig(const QString& configFile);
    void startUpdate();
    void retryUpdate();

private:
    std::unique_ptr<TrLoader> m_loader;
    QTimer m_scheduleTimer;
    QTimer m_retryTimer;
    
    // 配置信息
    QTime m_updateTime;
    int m_retryCount{3};
    int m_retryInterval{300};  // 秒
    int m_currentRetry{0};
    QString m_configFile;
    
    bool m_updating{false};
}; 